package com.zzsy.shoppingpro.user.framgent;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.base.BaseFragment;
import com.zzsy.shoppingpro.custom.CircleImageView;
import com.zzsy.shoppingpro.dialog.ComTwoBtnDialog;
import com.zzsy.shoppingpro.home.acitivty.HomeActivity;
import com.zzsy.shoppingpro.user.activity.LoginActivity;
import com.zzsy.shoppingpro.user.activity.MyInfoActivity;
import com.zzsy.shoppingpro.utils.ActivityCollector;
import com.zzsy.shoppingpro.utils.SPHelper;
import com.zzsy.shoppingpro.utils.StringUtils;

/**
 * Created by Administrator on 2019/8/17 0017.
 */

public class UserFragment extends BaseFragment implements View.OnClickListener {

    private CircleImageView ibUserIconAvator;
    private TextView tvUsername;
    private TextView tvAllOrder;
    private TextView tvUserPay;
    private TextView tvUserReceive;
    private TextView tvUserFinish;
    private TextView tvUserDrawback;
    private TextView tvUserLocation;
    private TextView tvUserCollect;
    private TextView tvUserCoupon;
    private TextView tvUserScore;
    private TextView tvUserPrize;
    private TextView tvUserTicket;
    private TextView tvUserInvitation;
    private TextView tvUserCallcenter;
    private TextView tvUserFeedback;
    private TextView tvUsercenter;
    private ImageButton ibUserSetting;
    private ImageButton ibUserMessage;
    private ScrollView scrollView;
    private ComTwoBtnDialog comTwoBtnDialog;

    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2016-10-08 09:07:21 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     *
     * @param view
     */
    private void findViews(View view) {
        ibUserIconAvator = view.findViewById(R.id.ib_user_icon_avator);
        tvUsername = view.findViewById(R.id.tv_username);
        tvAllOrder = view.findViewById(R.id.tv_all_order);
        tvUserPay = view.findViewById(R.id.tv_user_pay);
        tvUserReceive = view.findViewById(R.id.tv_user_receive);
        tvUserFinish = view.findViewById(R.id.tv_user_finish);
        tvUserDrawback = view.findViewById(R.id.tv_user_drawback);
        tvUserLocation = view.findViewById(R.id.tv_user_location);
        tvUserCollect = view.findViewById(R.id.tv_user_collect);
        tvUserCoupon = view.findViewById(R.id.tv_user_coupon);
        tvUserScore = view.findViewById(R.id.tv_user_score);
        tvUserPrize = view.findViewById(R.id.tv_user_prize);
        tvUserTicket = view.findViewById(R.id.tv_user_ticket);
        tvUserInvitation = view.findViewById(R.id.tv_user_invitation);
        tvUserCallcenter = view.findViewById(R.id.tv_user_callcenter);
        tvUserFeedback = view.findViewById(R.id.tv_user_feedback);
        tvUsercenter = view.findViewById(R.id.tv_usercenter);
        ibUserSetting = view.findViewById(R.id.ib_user_setting);
        ibUserMessage = view.findViewById(R.id.ib_user_message);
        scrollView = view.findViewById(R.id.scrollview);

        ibUserIconAvator.setOnClickListener(this);
    }

    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.fragment_user, null);
        findViews(view);
        return view;
    }

    @Override
    public void initData() {

        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int[] localtion = new int[2];
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE: //下滑是正，上滑是负
                        ibUserIconAvator.getLocationOnScreen(localtion); //初始值状态125 即最大值是125 全部显示不透明
                        float i = (localtion[1] - 10) / 85f;
                        tvUsercenter.setAlpha(1 - i);
                        break;
                }
                return false;
            }
        });

    }


    @Override
    public void onResume() {
        super.onResume();
        /**
         * 获取用户数据
         * */
        String jwt = (String) SPHelper.getSimpleParam(getContext(), "jwt", "");
        if (StringUtils.isEmpty(jwt)){
            showDialogs();
        }

    }

    private void showDialogs() {
        if(comTwoBtnDialog != null){
            comTwoBtnDialog.dismiss();
        }
        comTwoBtnDialog = new ComTwoBtnDialog(getActivity(),ComTwoBtnDialog.DIALOG_LOGIN);
        comTwoBtnDialog.show();
        comTwoBtnDialog.setIDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                comTwoBtnDialog.dismiss();
                startActivity(new Intent(getContext(), HomeActivity.class));
            }

            @Override
            public void onDialogRightBtnClick() {
                comTwoBtnDialog.dismiss();
                startActivity(new Intent(getContext(), LoginActivity.class));
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ib_user_icon_avator:
                startActivity(new Intent(getContext(), MyInfoActivity.class));
                break;
        }

    }
}
